﻿using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Design;
using Microsoft.Extensions.Configuration;
using System.IO;

namespace Batman.Cms.Core
{
    public class DbContextFactory : IDesignTimeDbContextFactory<CmsDbContext>
    {
        CmsDbContext IDesignTimeDbContextFactory<CmsDbContext>.CreateDbContext(string[] args)
        {
            //var jsonFile = "appsettings.Development.json";
            var jsonFile = "appsettings.json";

            IConfigurationRoot configuration = new ConfigurationBuilder()
               .SetBasePath(Directory.GetCurrentDirectory())
               .AddJsonFile(jsonFile)
               .Build();
            var builder = new DbContextOptionsBuilder<CmsDbContext>();
            var connectionString = configuration.GetConnectionString("DefaultConnection");
            builder.UseSqlServer(connectionString);

            return new CmsDbContext(builder.Options);
        }
    }
}
